import java.net.*;

/**
 * A simple controller for itunes.
 */
final class ITunesController {

  private final ApplicationController app = new ApplicationController(this,"iTunes");

  public void setDebug(boolean debug) {
    app.setDebug(debug);
  }

  // -------------------- Commands --------------------

  public void play() {
    exe("play");
  }

  public void playTrack(String name) {
    exe("play track \"" + URLDecoder.decode(name) + "\" of playlist \"Library\"");
  }

  public void play(String file) {
    exe("play " + Util.q(file));
  }

  public void open(String location) {
    exe("open location " + Util.q(location));
  }

  public void stop() {
    exe("stop");
  }

  public void pause() {
    exe("pause");
  }

  public void next() {
    exe("next track");
  }

  public void back() {
    exe("previous track");
  }

  public void playpause() {
    exe("playpause");
  }

  public void setVolume(int percent) {
    if (percent < 0 || percent > 100) {
      throw new IllegalArgumentException("percent:" + percent + "must be between 0 and 100");
    }
    exe("set sound volume as integer");
    exe("set sound volume to " + percent);
  }

  public String getStatus() {
    String state = exe("player state as string");
    if (state.toLowerCase().equals("playing")) {
      String artist = exe("artist of current track as string");
      String track = exe("name of current track as string");
      return "Playing " + track + " by " + artist;
    }
    return state;
  }

  // -------------------- Private --------------------

  /** Shortcut for <code>app.execute(line)</code>. */
  private String exe(String line) {
    return app.execute(line);
  }

  /** Shortcut for <code>app.execute(lines)</code>. */
  private String exe(String[] lines) {
    return app.execute(lines);
  }

  public static void main(String[] args) {
    try {
      new ITunesController().app.realMain(args);
    } catch (java.io.IOException e) {e.printStackTrace();}
  }
}